<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 组织机构ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysDepartment.class.php');

class ajax_userDepartment {

    public function execute(){

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1://添加部门组织
                    $this->addUserDepartment();
                    break;
                case 2: //改变禁用启用状态
                    $this->changeStatue();
                    break;
                case 3: //子分类上级部门名称查询
                    $this->searchUserDepartment();
                    break;
                case 4: //修改部门信息
                    $this->editUserDepartment();
                    break;
                case 5: //删除部门信息
                    $this->delUserDepartment();
                    break;
            }

        }

    }

    /*
     *
     * 添加部门组织
     */
    private function addUserDepartment() {

        global $G_PGLOB;
//        echo '<pre>';
//        print_r($_REQUEST);exit;
        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
        $_REQUEST['departmentName'] = $_REQUEST['pop_departmentName'];
        //实例部门类
        $sysDepartment = new sysDepartment($G_PGLOB->DBCA);

        //保存部门组织
        $result_array = $sysDepartment->saveUserdepartment( $_REQUEST ) ;

        returnJsonData($result_array['message'], $result_array['code']);

    }


    /*
     *
     * 改变禁用启用状态
     */
    public function changeStatue(){

        global $G_PGLOB;

        //实例部门类
        $sysDepartment = new sysDepartment($G_PGLOB->DBCA);

        //改变部门组织禁用启用方法
        $result_array = $sysDepartment->changeStatue( $_REQUEST ) ;

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /*
     *
     * 子分类上级部门名称查询
     */
    public function searchUserDepartment(){

        global $G_PGLOB;

        if(!isset($_REQUEST['departmentID']) || !is_numeric($_REQUEST['departmentID'])){
            echo json_encode(array('code' => 400, 'message' => '参数错误' )) ;
            exit;
        }

        $change_department = new entity('user_department');
        $change_department->departmentID = $_REQUEST['departmentID'];

        if (!$G_PGLOB->DBCA->fetch($change_department)) {
            echo json_encode(array('code' => 400, 'message' => '部门信息未找到，请核实')) ;
            exit;
        }

        $departmentName = $change_department->departmentName;
        if($departmentName){
            echo json_encode(array('code' => 200, 'message' => '','data'=>$departmentName)) ;
            exit;
        }
    }

    /*
     *
     * 修改部门信息
     */
    public function editUserDepartment(){

        global $G_PGLOB;

        //实例部门类
        $sysDepartment = new sysDepartment( $G_PGLOB->DBCA );

        //修改部门组织
        $result_array = $sysDepartment->editUserDepartment( $_REQUEST ) ;

        returnJsonData($result_array['message'], $result_array['code']);
    }


    /*
     *
     * 删除部门信息
     */
    public function delUserDepartment(){

        global $G_PGLOB;

        //实例部门类
        $sysDepartment = new sysDepartment( $G_PGLOB->DBCA );

        //删除部门信息方法
        $result_array = $sysDepartment->delUserDepartment( $_REQUEST ) ;

        returnJsonData($result_array['message'], $result_array['code']);
    }
}